package crosstime.android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import crosstime.android.interfaces.ISelectController;

public class SelectLayerWidget extends View {
	private ISelectController mListController = null;
	
	private boolean mDragStarted = false;
	private Rect mSelectedRect = null;
	private Paint mPaint = null;
	private Path mPath = null;
	private int mLastX, mLastY;
	private final static int TOUCH_TOLERANCE = 5;
	public SelectLayerWidget(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public SelectLayerWidget(Context context) {
		super(context);
		init();
	}

	private void init(){
		mDragStarted = false;
		mSelectedRect = new Rect();
		
		mPath = new Path();
		mPaint = new Paint();

		mPaint.setARGB(127, 0, 0, 0);
		mPaint.setStrokeWidth(4F);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setAntiAlias(true);
        
        DashPathEffect dashPath = new DashPathEffect(new float[]{12,8}, 0);
        mPaint.setPathEffect(dashPath);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		mPath.reset();
		mPath.addRect(new RectF(mSelectedRect), Path.Direction.CW);
		canvas.drawPath(mPath, mPaint);
		super.onDraw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(mListController == null || mListController.getMode() == ISelectController.MODE_OFF)
			return super.onTouchEvent(event);
		switch(event.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			mDragStarted = true;
			mLastX = (int) event.getX();
			mLastY = (int) event.getY();
			mSelectedRect.set(mLastX, mLastY, mLastX, mLastY);
//			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			int x = (int) event.getX();
			int y = (int) event.getY();
			if (mDragStarted && 
					(Math.abs(x - mLastX) > TOUCH_TOLERANCE || 
							Math.abs(y - mLastY) > TOUCH_TOLERANCE)) {
				mSelectedRect.right = x;
				mSelectedRect.bottom = y;
				invalidate();
			}
			break;
			
		case MotionEvent.ACTION_UP:
			if(mListController != null)
			{
				adjustRect(mSelectedRect);
				mListController.select(mSelectedRect);
			}
			
			mSelectedRect.set(0,0,0,0);
			mDragStarted = false;
			
			invalidate();
			break;
			
		default:
			break;
		}
		if(mDragStarted)
			return true;
		else
			return super.onTouchEvent(event);
	}

    public void setController(ISelectController controller) {    
        mListController = controller;    
    } 
	
	//adjust rect to normal express
	//left < right, top < bottom
	private void adjustRect(Rect rect){
		int tmp = 0;
		if(rect.left > rect.right)
		{
			tmp = rect.left;
			rect.left = rect.right;
			rect.right = tmp;
		}
		if(rect.top > rect.bottom)
		{
			tmp = rect.top;
			rect.top = rect.bottom;
			rect.bottom = tmp;
		}
	}
}
